<?php

/**
 * DISPENDA2BPD WEBSERVICE
 *
 * An close source application development
 *
 * @package		DISPENDA GIANYAR WEBSERVICE
 * @author		Andi Kurniawan
 * @copyright           PT. RUMAH LINUX YOGYAKARTA (c) 2013.
 * @license		nakamichikun@gmail.com
 * @follow		@nakamichikun
 * @since		Version 1.0
 * @filesource
 */
class Gui extends CI_Controller {

    function __construct() {
        parent::__construct();
        set_time_limit(0);
        ini_set('memory_limit', '1024M');
        ini_set('default_socket_timeout', 1000000);
        $this->load->model('modnop');
    }

    function index() {
        $this->load->view('template/header-land');
        $this->load->view('template/menu');
        $this->load->view('content/base');
        $this->load->view('template/footer');
    }

    function home() {
        $this->load->view('template/header-auto');
        $this->load->view('template/menu');
        $this->load->view('content/base');
        $this->load->view('template/footer');
    }

    function delete() {
        $this->load->view('template/header-auto');
        $this->load->view('template/menu');
        $this->load->view('content/delete');
        $this->load->view('template/footer');
    }

    function penetapan() {
        $this->load->view('template/header-auto');
        $this->load->view('template/menu');
        $query = "SELECT * FROM bphtb_dev WHERE status_kirim=0 AND ROWNUM<=20 ORDER BY ID"; //uji coba mengambil 5 data
        $getpenetapan = $this->modnop->umum($query); //mencari data didatabase berdasarkan tahun inputan
        require_once 'webservice.php';
        $ws = new Webservice();
        for ($i = 0; $i < count($getpenetapan); $i++) {
            $delete = $ws->tagihanDeleteById($getpenetapan[$i]->ID);
            if ($delete->code == '00') {
                $ins = array(
                    'id' => $getpenetapan[$i]->ID, //TIB yang didapat dari database
                    'nama' => $getpenetapan[$i]->NAMA, //NAMA WP yang didapat dari database
                    'jumlah' => $getpenetapan[$i]->JUMLAH_PAJAK, //Jumlah pajak BPHTB yang didapat dari database
                    'instansi' => $getpenetapan[$i]->INSTANSI, //Instansi yang didapat dari database
                    'nop' => $getpenetapan[$i]->NOP //nop yang didapat dari database
                );
                $obj = (object) $ins;
                $ist = $this->tagihanInsert($obj);
                if ($ist->code == '00') {
                    $qry = "UPDATE BPHTB_DEV SET status_kirim='1' WHERE ID=" . $getpenetapan[$i]->ID;
                    $this->modnop->general($qry);
                }
            }
        }
        $que = "SELECT * FROM bphtb_dev WHERE status_kirim=0 ORDER BY ID"; //uji coba mengambil 5 data
        $data['output'] = $this->modnop->umum($que); //mencari data didatabase berdasarkan tahun inputan
        $this->load->view('content/outputPenetapan', $data);
        $this->load->view('template/footer');
    }

    function getDataWpPenetapan($nop, $tahun) {
        $data = $this->modnop->getDataWpPenetapan($nop, $tahun); //mengambil data wp dari database oracle
        $datawp = $data[0];
        return $datawp;
    }

    function tagihanInsert($datawp) {
        require_once 'webservice.php'; //memanggil file
        $ws = new Webservice(); //memanggil class webservice dengan membuat object baru
        try {
            $insert = $ws->tagihanInsert($datawp);
            return $insert;
        } catch (Exception $ex) {
            echo '<pre>';
            print_r($ex);
            echo '</pre>';
        }
    }

    function deleteInstansi() {
        require_once 'webservice.php'; //memanggil file
        $ws = new Webservice(); //memanggil class webservice dengan membuat object baru
        try {
            $delete = $ws->tagihanDeleteInstansi();
            return $delete;
        } catch (Exception $ex) {
            echo '<pre>';
            print_r($ex);
            echo '</pre>';
        }
    }

    function reportPayment() {
        $this->load->model('modsvc');
        $this->load->view('template/header-auto');
        $this->load->view('template/menu');
        try {
            require_once 'webservice.php';
            $ws = new Webservice();
            $report['info'] = $ws->laporanPaymentDetail();
            $nop = $report['info']->no_id;
            $tahun = $report['info']->Tahun;
            $reversal = $report['info']->sts_reversal;
            if ($nop != NULL && $tahun != NULL && $reversal == 0) {
                $this->modsvc->updateSppt($nop, $tahun);
            } elseif ($nop != null && $tahun != null && $reversal == 1) {
                $this->modsvc->updateSpptReversal($nop, $tahun);
            }
            $this->load->view('content/tarikdata', $report);
        } catch (Exception $ex) {
            $this->load->view('content/tarikdata', $ex);
        }
        $this->load->view('template/footer');
    }

    function reportPaymentManual() {
        $this->load->model('modsvc');
        $this->load->view('template/header');
        $this->load->view('template/menu');
        if (!$_POST) {
            $this->load->view('content/date');
        } else {
            try {
                $date = $this->input->post('date');
                require_once 'webservice.php';
                $ws = new Webservice();
                $report['info'] = $ws->laporanPaymentDetailManual($date);
                $nop = $report['info']->no_id;
                $tahun = $report['info']->Tahun;
                $reversal = $report['info']->sts_reversal;
                if ($nop != NULL && $tahun != NULL && $reversal == 0) {
                    $this->modsvc->updateSppt($nop, $tahun);
                } elseif ($nop != null && $tahun != null && $reversal == 1) {
                    $this->modsvc->updateSpptReversal($nop, $tahun);
                }
                $this->load->view('content/tarikdata', $report);
            } catch (Exception $ex) {
                $this->load->view('content/tarikdata', $ex);
            }
        }
        $this->load->view('template/footer');
    }

    function inquery() {
        $this->load->view('template/header');
        $this->load->view('template/menu');
        if ($_POST) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('nop', 'NOP', 'required');
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('content/inqnop');
            } else {
                require_once 'webservice.php';
                try {
                    $con = new Webservice();
                    $nop = trim($this->input->post('nop'));
                    $inq = $con->inquery($nop);
                    if (isset($inq->Table1)) {
                        $tbl = $inq->Table1;
                        $data['tipe'] = 'danger';
                        $data['message'] = $tbl->message;
                        $this->parser->parse('content/message', $data);
                    } else {
                        $data['message'] = $inq->Table;
                        $this->load->view('content/outinqnop', $data);
                    }
                } catch (Exception $ex) {
                    echo '<pre>';
                    print_r($ex);
                    echo '</pre>';
                }
            }
        } else {
            $this->load->view('content/inqnop');
        }
        $this->load->view('template/footer');
    }

}
